/* rcsoiscd.p - Release Management Customer Schedules                     */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                    */
/* All rights reserved worldwide.  This is an unpublished work.           */
/* $Revision: 1.9 $            */
/*                                                                        */
/* Revision: 1.6    BY: Dan Herman    DATE: 07/16/01   ECO: *P006* */
/* Revision: 1.8    BY: Dan Herman    DATE: 08/15/01   ECO: *P01L* */
/* $Revision: 1.9 $       BY: Dan Herman       DATE: 9/03/02 ECO: *P0HG*  */

/*V8:ConvertMode=NoConvert                                                */
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */

         /* SHIPPER CONFIRM SUBPROGRAM */
/*------------------------------------------------------------------------
Purpose:       Create Sales Order Detail Container Charges records and
               create self billing records .

Exceptions:    None
Conditions:
Pre:           Container and Line Charges must be enabled.
Post:
Notes:
History:

Inputs:
ipShipFrom     (character)
ipShipTo       (character)
ipShipDate      (date)
ipEffectiveDate     (date)
ipAbsRecid    (recid)
ipConfirmMode (logical)
ipAutoPost    (logical)
ipForASN      (logical)

Outputs:

----------------------------------------------------------------------------*/

{mfdeclre.i}
{pxpgmmgr.i}
{pxphdef.i cclcxr}

define input parameter ipShipFrom like abs_shipfrom no-undo.
define input parameter ipShipTo like abs_shipto no-undo.
define input parameter ipShipDate like abs_shp_date no-undo.
define input parameter ipEffectiveDate like abs_shp_date no-undo.
define input parameter ipAbsRecid as recid no-undo.
define input parameter ipConfirmMode like mfc_logical no-undo.
define input parameter ipAutoPost like mfc_logical no-undo.
define input parameter ipForASN like mfc_logical no-undo.

define variable par_id as character no-undo.

define variable sales_order like sod_nbr no-undo.
define variable order_line like sod_line no-undo.
define variable charge_type like abs_charge_type no-undo.
define variable container_price like abs_price no-undo.
define variable container_unit_price like abs_price no-undo.
define variable avail_sod like mfc_logical no-undo.
define variable sod_recid as recid no-undo.
define variable so_mstr_recid as recid no-undo.
define variable currency as character no-undo.
define variable vFreightClass as character no-undo.
define variable vShipWeight as integer no-undo.
define variable vShipWeightUm as character no-undo.
define variable vSalesAccount as character no-undo.
define variable vSubAccount as character no-undo.
define variable vCostCenter as character no-undo.
define variable vTaxable as logical no-undo.
define variable vTaxClass as character no-undo.
define variable vChargeable as logical no-undo.
define variable op_return_status as integer no-undo.
define variable vSoTaxable as logical no-undo.
define variable vSelfBill as logical no-undo.
define variable vQuantity as decimal no-undo.

if can-find(first sbic_ctl no-lock where
   sbic_active = yes) then vSelfBill = yes.
else vSelfBill = no.

for first abs_mstr exclusive-lock
      where recid(abs_mstr) = ipAbsRecid:

   par_id = abs_id.

   if abs_id begins "c" then do:

      assign
         vQuantity = abs_qty
         sales_order = ""
         order_line = 0
         avail_sod = no
         sod_recid = ?
         so_mstr_recid = ?
         currency = ""
         vSoTaxable = no.

      for first sod_det no-lock where
            sod_nbr = abs_order  and
            sod_line = integer(abs_line):

         assign
            sales_order = sod_nbr
            order_line = sod_line
            avail_sod = yes
            sod_recid = recid(sod_det).

         for first so_mstr no-lock where
               so_nbr = sod_nbr:
            so_mstr_recid = recid(so_mstr).
            assign
               vSoTaxable = so_taxable
               currency = so_curr.
         end.
      end.

      if not available sod_det then do:

         {gprun.i ""rcsoisce.p""
            "(input abs_id,
                    input ipShipFrom,
                    output sales_order,
                    output order_line)"}

         for first sod_det no-lock where
               sod_nbr = sales_order  and
               sod_line = order_line:

            assign
               avail_sod = yes
               sod_recid = recid(sod_det).
            for first so_mstr no-lock where
                  so_nbr = sod_nbr:
               assign
                  currency = so_curr
                  so_mstr_recid = recid(so_mstr).
            end.
         end. /* FOR FIRST SOD_DET */
      end. /* IF NOT AVAILABLE SOD_DET */

      for first abscc_det exclusive-lock
            where abscc_abs_id = abs_id
            and   abscc_abs_shipfrom = abs_shipfrom:

         if (ipConfirmMode = no  and
             ipForASN = no) then
            /* Unconfirm the shipper */
            assign
               vQuantity = abs_qty * -1
               abscc_inv_post = no
               abscc_inv_nbr = "".
         abscc_qty = if ipAutoPost then vQuantity else (abscc_qty + vQuantity).
      end.

      if not available abscc_det then do:
         create abscc_det.
         assign
            abscc_abs_id = abs_id
            abscc_abs_shipfrom = abs_shipfrom
            abscc_qty = abs_qty.
         if recid(abscc_det) = -1 then.
      end.

      {pxrun.i &PROC = 'getContainerInvoiceData'
         &PROGRAM = 'cclcxr.p'
         &HANDLE = ph_cclcxr
         &PARAM = "(input abs_item,
                    input vSoTaxable,
                    output vFreightClass,
                    output vShipWeight,
                    output vShipWeightUm,
                    output vSalesAccount,
                    output vSubAccount,
                    output vCostCenter,
                    output vTaxable,
                    output vTaxClass,
                    output vChargeable)"
      }
      assign
         abscc_container   = abs_item
         abscc_shipto      = ipShipTo
         abscc_shp_date    = ipShipDate
         abscc_cont_price  = 0
         abscc_charge_type = ""
         abscc_order       = sales_order
         abscc_ord_line    = order_line
         abscc_fr_class    = vFreightClass
         abscc_ship_wt     = vShipWeight
         abscc_ship_wt_um  = vShipWeightUm
         abscc_sls_acct    = vSalesAccount
         abscc_sls_sub     = vSubAccount
         abscc_sls_cc      = vCostCenter
         abscc_taxable     = vTaxable
         abscc_taxc        = vTaxClass.

      if ipConfirmMode then
         abscc_confirmed = yes.

      if vChargeAble then do:
         {pxrun.i &PROC = 'getContainerChargeType'
            &PROGRAM = 'cclcxr.p'
            &HANDLE = ph_cclcxr
            &PARAM = "(input avail_sod,
                       input sod_recid,
                       input abs_item,
                       input ipShipTo,
                       input currency,
                       output charge_type)"
            }

         {pxrun.i &PROC = 'getContainerUnitPrice'
            &PROGRAM = 'cclcxr.p'
            &HANDLE = ph_cclcxr
            &PARAM = "(input ipShipTo,
                       input abs_item,
                       input currency,
                       input ipEffectiveDate,
                       input sales_order,
                       output container_unit_price)"
         }

         {pxrun.i &PROC  = 'updateChargeTypePrice'
            &PROGRAM = 'cclcxr.p'
            &HANDLE = ph_cclcxr
            &PARAM = "(input charge_type,
                       input yes,
                       input-output container_unit_price,
                       input so_mstr_recid,
                       input sod_recid,
                       input ?,
                       input ipAbsRecid,
                       input ?,
                       output container_price)"}

         assign
            abscc_cont_price  = container_price
            abscc_charge_type = charge_type
            abs_mstr.abs_charge_type = charge_type
            abs_mstr.abs_price = container_price.

         if ipForASN = no and vSelfBill = yes then do:

            /* CREATE SELF-BILLING RECORD */
            {gprun.i ""arsixcr4.p""
               "(input ipAbsRecid,
                 input ipConfirmMode,
                 output op_return_status)"}
         end.
      end. /* IF vChargeAble */
   end. /* IF ABS_ID BEGINS "C" */
end. /*FOR FIRST abs_mstr*/

release abscc_det.
release abs_mstr.

for each abs_mstr
   where abs_mstr.abs_shipfrom = ipShipFrom
   and abs_mstr.abs_par_id = par_id no-lock:

   {gprun.i ""rcsoiscd.p""
      "(input ipShipFrom,
        input ipShipTo,
        input ipShipDate,
        input ipEffectiveDate,
        input recid(abs_mstr),
        input ipConfirmMode,
        input ipAutoPost,
        input ipForASN)"}
end.
